import 'dart:io';

import 'package:hongsheng/splash_screen.dart';
import 'package:hongsheng/utils/LocalStorage.dart';
import 'package:hongsheng/utils/SPClassToastUtils.dart';
import 'package:hongsheng/utils/colors.dart';
import 'package:hongsheng/utils/common.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:google_fonts/google_fonts.dart';

import 'app/SPClassApplicaion.dart';
import 'init_page.dart';

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await LocalStorage.getInstance();
  runApp(MyApp());
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown
  ]);
  if (Platform.isAndroid) {
    // 以下两行 设置android状态栏为透明的沉浸。写在组件渲染之后，是为了在渲染后进行set赋值，覆盖状态栏，写在渲染之前MaterialApp组件会覆盖掉这个值。
    SystemUiOverlayStyle systemUiOverlayStyle =
    SystemUiOverlayStyle(statusBarColor: Colors.transparent,statusBarIconBrightness: Brightness.light);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => MyAppState();
}


class MyAppState extends State<MyApp> {
  var spProPopTimer= DateTime.now();

  @override
  Widget build(BuildContext context) {

    return MaterialApp(
        title: SPClassApplicaion.spProAppName,
        theme: ThemeData(
          brightness: Brightness.light,
          primaryColor: MyColors.main1,
          backgroundColor: Colors.white,
          unselectedWidgetColor: Colors.white70,
          accentColor: const Color(0xFF888888),
          textTheme: GoogleFonts.notoSansTextTheme(),
          iconTheme: const IconThemeData(
            color: MyColors.main1,
            size: 35.0,
          ),
          appBarTheme: AppBarTheme(
            backgroundColor: MyColors.main1
          ),
        ),
        localizationsDelegates: const [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ],
        supportedLocales: const [
          Locale('zh', 'CH'),
          Locale('en', 'US'),
        ],
        home: CupertinoPageScaffold(
          child:
          WillPopScope(
            child:  Scaffold(body:LocalStorage.get(Commons.IS_AGREE_PRIVICY)!=null?SplashScreen():const InitPage()),
            onWillPop: () async{
              if(DateTime.now().difference(spProPopTimer).inSeconds>3){
                SPClassToastUtils.spFunShowToast(msg: "再按一次退出");
              }else{
                return true;
              }
              spProPopTimer=DateTime.now();
              return false;
            },
          ),
        ),
    );
  }
}
